package com.nus.fellowmark.modules.assignment.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.entity.PairingEntity;
import com.nus.fellowmark.modules.assignment.dto.DeleteByIdsDTO;
import com.nus.fellowmark.modules.assignment.dto.PairingDTO;
import com.nus.fellowmark.modules.assignment.service.PairingService;
import com.nus.fellowmark.modules.assignment.vo.PairingVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nus.fellowmark.common.utils.PageUtils;



/**
 * 
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-02-10  
 */
@RestController
@RequestMapping("pairing")
public class PairingController {
    @Autowired
    private PairingService pairingService;

    @ApiOperation("list pairing")
    @RequestMapping("/list")
    public ServerResponseEntity list(@RequestParam Map<String, Object> params){
        PageUtils page = pairingService.queryPage(params);

        return ServerResponseEntity.success("page", page);
    }


    @ApiOperation("select pairing by id ")
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Integer id){
		PairingEntity pairing = pairingService.getById(id);

        return ServerResponseEntity.success("pairing", pairing);
    }

    @ApiOperation("save pairing")
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody PairingEntity pairing){
		pairingService.save(pairing);

        return ServerResponseEntity.success();
    }

    @ApiOperation("update pairing")
    @RequestMapping("/update")
    public ServerResponseEntity update(@RequestBody PairingEntity pairing){
		pairingService.updateById(pairing);

        return ServerResponseEntity.success();
    }

    @ApiOperation("delete pairing")
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody DeleteByIdsDTO deleteByIdsDTO){
		pairingService.removeByIds(Arrays.asList(deleteByIdsDTO.getIds()));

        return ServerResponseEntity.success();
    }

    @ApiOperation("Pairing students by assignmentId and pair-group nums")
    @RequestMapping("/pair")
    public ServerResponseEntity pair(@RequestBody PairingDTO pairingDTO) {
        List<PairingVO> ps = pairingService.listPairsOfAssignment(pairingDTO.getAssignmentId());
        if (!ps.isEmpty()) {
            return ServerResponseEntity.showFailMsg("Already pair");
        }
        Boolean pairResult = pairingService.pair(pairingDTO);
        if (!pairResult) {
            return ServerResponseEntity.showFailMsg("Fail to pair students");
        }
        List<PairingVO> pairs = pairingService.listPairsOfAssignment(pairingDTO.getAssignmentId());
        if (pairs == null || pairs.size() == 0) {
            return ServerResponseEntity.showFailMsg("No pairs");
        }
        return ServerResponseEntity.success(pairs);
    }

    @ApiOperation("List pairs by assignmentId")
    @RequestMapping("/listPairsByAssignmentId/{assignmentId}")
    public ServerResponseEntity listPairsByAssignmentId(@PathVariable Integer assignmentId) {
        List<PairingVO> pairs = pairingService.listPairsOfAssignment(assignmentId);
        return ServerResponseEntity.success(pairs);
    }

}
